import Vue from 'vue'

Vue.prototype.$toPage =toPage
Vue.prototype.$back = back
Vue.prototype.$redirectTo = redirectTo
Vue.prototype.$setStorage = setStorage
Vue.prototype.$getStorage = getStorage
Vue.prototype.$showToast = showToast
Vue.prototype.$showModal = showModal
Vue.prototype.$byteFormat = byteFormat
Vue.prototype.$hideKeyboard = hideKeyboard

    //收键盘
    function  hideKeyboard() {
        uni.hideKeyboard()
      }
//换算byte为mb，大于1024mb的换算成gb
function byteFormat(num) {
	if (typeof num !== 'number') {
		num = parseInt(num);
	}
	let mb = (num / 1024 / 1024).toFixed(2)
	if (mb >= 1024) {
		return (mb / 1024).toFixed(2) + 'G'
	}
	return mb + 'M';
}
function toPage(url) {
	uni.navigateTo({
		'url': url,
		'animation-type': "pop-in",
		'animationDuration': 200
	})
	// const userRoute = ['pages/index', '/pages/user/index']
	// if (userRoute.indexOf(url) != -1) { //进入需要用户信息的页面时刷新
	// 	uni.showLoading({
	// 		title: '加载中'
	// 	});
	// 	await api.user.getUserInfo().then(res => {
	// 		console.log(res.data)
	// 		if (res.code == 200) {
	// 			setStorage('userInfo', res.data)
	// 		}
	// 	})
	// 	uni.hideLoading()
	// }
}

function redirectTo(url) {
	uni.redirectTo({
	'url': url,
	'animation-type': "pop-in",
	'animationDuration': 200
	})
}
function back(delta=1) {
	uni.navigateBack({
		'delta': delta,
		'animation-type': "pop-out",
		'animationDuration': 200
	})
}

function setStorage(key, data) {
  uni.setStorageSync(key,data)
}

function getStorage(key) {
 return uni.getStorageSync(key)
}

function showToast(msg, duration, icon,image,mask) {
	uni.showToast({
		title: msg,
		icon: icon || 'none',
		duration: duration || 2000,
    image:image||'',
    mask:mask||false
	})
}

function showModal(title, content, success) {
	uni.showModal({
		title: title,
		content: content,
		confirmColor: '#3D9FEA',
		cancelColor: '#000000',
		success(res) {
			success(res)
		}
	})
}